package org.nlp.dictionary.maxword;

public final class TrieNode {

	protected TrieNode[] children;
	protected char splitChar; 
	protected String area;
	
	
	protected TrieNode(char splitchar) {
		children = new TrieNode[10];
		area = null;
		this.splitChar = splitchar;
	}
	
	public void addWord(String string, TrieNode root, String area) {
		TrieNode tstNode = root;
		for (int i = 0; i < string.length(); i++) {
			char c0 = string.charAt(i);
			int ind = Integer.parseInt(string.substring(i,i+1));
			TrieNode tmpNode = tstNode.children[ind];
			if (null == tmpNode){
				tmpNode = new TrieNode(c0);				
			}
			if (i == string.length()-1){
				tmpNode.area = area;
			}
			tstNode.children[ind] = tmpNode;
			tstNode = tmpNode;
			
		}
	}
	
	public String search(String tel) {
		TrieNode root = null;
		TrieNode tstNode = root;
		for (int i = 0; i < tel.length(); i++) {
			tstNode = tstNode.children[(tel.charAt(i)-'0')];
			if (null != tstNode.area){
				return tstNode.area;
			}
		}
		return null;
		
	}
}
